package com.cyl.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.wms.domain.Container;
import com.cyl.wms.domain.Consolidate;
import com.cyl.wms.domain.Inventory;
import com.cyl.wms.domain.Item;
import com.cyl.wms.mapper.ContainerMapper;
import com.cyl.wms.mapper.ConsolidateMapper;
import com.cyl.wms.mapper.InventoryMapper;
import com.cyl.wms.mapper.ItemMapper;
import com.cyl.wms.pojo.query.ConsolidateQuery;
import com.cyl.wms.pojo.vo.ConsolidateVO;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 地址码Service业务层处理
 *
 * @author Lisi
 */
@Service
public class ConsolidateService {
    @Autowired
    private ConsolidateMapper consolidateMapper;

    /**
     * 查询货区
     *
     * @param id 货区主键
     * @return 货区
     */
    public Consolidate selectById(Long id) {
        return consolidateMapper.selectById(id);
    }

    /**
     * 查询货区列表
     *
     * @param query 查询条件
     * @param page  分页条件
     * @return 货区
     */
    public List<Consolidate> selectList(ConsolidateQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<Consolidate> qw = new QueryWrapper<>();
        qw.eq("del_flag", 0);
        String addrNo = query.getAddrNo();
        if (!StringUtils.isEmpty(addrNo)) {
            qw.eq("addr_no", addrNo);
        }
        String addrNameLike = query.getAddrNameLike();
        if (!StringUtils.isEmpty(addrNameLike)) {
            qw.like("area_name", addrNameLike);
        }
        Long warehouseId = query.getWarehouseId();
        if (warehouseId != null) {
            qw.eq("warehouse_id", warehouseId);
        }
        return consolidateMapper.selectList(qw);
    }

    /**
     * 新增货区
     *
     * @param icqa 货区
     * @return 结果
     */
    public int insert(Consolidate icqa) {
        icqa.setDelFlag(0);
        icqa.setCreateTime(LocalDateTime.now());
        return consolidateMapper.insert(icqa);
    }

    /**
     * 修改货区
     *
     * @param icqa 货区
     * @return 结果
     */
    public int update(Consolidate icqa) {
        return consolidateMapper.updateById(icqa);
    }

    /**
     * 批量删除货区
     *
     * @param ids 需要删除的货区主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        return consolidateMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除货区信息
     *
     * @param id 货区主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return consolidateMapper.updateDelFlagByIds(ids);
    }

    public List<Consolidate> selectByIdIn(Collection<Long> ids) {
        QueryWrapper<Consolidate> qw = new QueryWrapper<>();
        qw.in("id", ids);
        return consolidateMapper.selectList(qw);
    }

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private ContainerMapper containerMapper;

    public List<ConsolidateVO> convert2vo(List<Consolidate> list) {
        List<ConsolidateVO> vos = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Consolidate consolidate = list.get(i);
            ConsolidateVO vo = new ConsolidateVO();
            BeanUtils.copyProperties(consolidate,vo);
            Inventory inventoryFrom = this.inventoryMapper.selectById(consolidate.getInvFrom());
            Item item = this.itemMapper.selectById(inventoryFrom.getItemId());
            vo.setItemName(item.getItemName());
            vo.setItemNo(item.getItemNo());
            Container containerFrom = containerMapper.selectById(inventoryFrom.getContainerId());
            vo.setContainerNoFrom(containerFrom.getContainerNo());

            Inventory inventoryTo = this.inventoryMapper.selectById(consolidate.getInvFrom());
            Container containerTo = containerMapper.selectById(inventoryTo.getContainerId());
            vo.setContainerNoTo(containerTo.getContainerNo());

            vo.setInvMove(consolidate.getInvMove());

            vos.add(vo);
        }
        return vos;
    }
}
